package com.itcat.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.*;
import com.itcat.common.CommonPage;
import com.itcat.mysql.param.PmsProductParam;
import com.itcat.mysql.pojo.*;
import com.itcat.service.PmsProductService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @author Jerry Broker
 * @project_name rainbow-mall
 * @create 2022-04-02 16:20
 */
@Service
public class PmsProductServiceImpl implements PmsProductService {

    @DubboReference
    private PmsProductApi pmsProductApi;

    @DubboReference
    private PmsMemberPriceApi pmsMemberPriceApi;

    @DubboReference
    private CmsPrefrenceAreaProductRelationApi cmsPrefrenceAreaProductRelationApi;

    @DubboReference
    private PmsProductAttributeValueApi pmsProductAttributeValueApi;

    @DubboReference
    private PmsProductFullReductionApi pmsProductFullReductionApi;

    @DubboReference
    private PmsProductLadderApi pmsProductLadderApi;

    @DubboReference
    private PmsSkuStockApi pmsSkuStockApi;

    @DubboReference
    private CmsSubjectProductRelationApi cmsSubjectProductRelationApi;

    @DubboReference
    private PmsProductCategoryApi pmsProductCategoryApi;

    /**
     * 查询商品信息
     *
     * @param pmsProductParam
     * @return
     */
    @Override
    public CommonPage<PmsProduct> list(PmsProductParam pmsProductParam) {
        Page<PmsProduct> page = pmsProductApi.list(pmsProductParam);
        CommonPage<PmsProduct> restPage = CommonPage.restPage(page);
        return restPage;
    }

    /**
     * 创建商品
     *
     * @param pmsProductParam
     */
    @Override
    public void create(PmsProductParam pmsProductParam) {
        // 创建并保存商品信息
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(pmsProductParam, pmsProduct);
        // 保存商品信息并返回商品id
        Long productId = pmsProductApi.save(pmsProduct);

        // 保存商品会员价格设置
        List<PmsMemberPrice> memberPriceList = pmsProductParam.getMemberPriceList();
        for (PmsMemberPrice price : memberPriceList) {
            price.setProductId(productId);
        }
        if (!memberPriceList.isEmpty())
            pmsMemberPriceApi.saveList(memberPriceList);

        // 保存优选专区和商品的关系
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = pmsProductParam.getPrefrenceAreaProductRelationList();
        for (CmsPrefrenceAreaProductRelation relation : prefrenceAreaProductRelationList) {
            relation.setProductId(productId);
        }
        if (!prefrenceAreaProductRelationList.isEmpty())
            cmsPrefrenceAreaProductRelationApi.saveList(prefrenceAreaProductRelationList);

        // 保存商品参数及自定义规格属性
        List<PmsProductAttributeValue> productAttributeValueList = pmsProductParam.getProductAttributeValueList();
        for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
            pmsProductAttributeValue.setProductId(productId);
        }
        if (!productAttributeValueList.isEmpty())
            pmsProductAttributeValueApi.saveList(productAttributeValueList);

        // 保存商品满减价格设置
        List<PmsProductFullReduction> productFullReductionList = pmsProductParam.getProductFullReductionList();
        for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
            pmsProductFullReduction.setProductId(productId);
        }
        if (!productFullReductionList.isEmpty())
            pmsProductFullReductionApi.saveList(productFullReductionList);

        // 保存商品阶梯价格设置
        List<PmsProductLadder> productLadderList = pmsProductParam.getProductLadderList();
        for (PmsProductLadder pmsProductLadder : productLadderList) {
            pmsProductLadder.setProductId(productId);
        }
        if (!productLadderList.isEmpty())
            pmsProductLadderApi.saveList(productLadderList);

        // 保存商品的sku库存信息
        List<PmsSkuStock> skuStockList = pmsProductParam.getSkuStockList();
        for (PmsSkuStock pmsSkuStock : skuStockList) {
            pmsSkuStock.setProductId(productId);
        }
        if (!skuStockList.isEmpty())
            pmsSkuStockApi.saveList(skuStockList);

        // 保存专题和商品关系
        List<CmsSubjectProductRelation> subjectProductRelationList = pmsProductParam.getSubjectProductRelationList();
        for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
            cmsSubjectProductRelation.setProductId(productId);
        }
        if (!subjectProductRelationList.isEmpty())
            cmsSubjectProductRelationApi.saveList(subjectProductRelationList);
    }

    /**
     * 更改删除状态
     *
     * @param ids
     * @param deleteStatus
     */
    @Override
    public void updateDeleteStatus(Integer[] ids, Integer deleteStatus) {
        List<Integer> list = Arrays.asList(ids);
        pmsProductApi.updateDeleteStatus(list, deleteStatus);

    }

    /**
     * 批量设为新品
     *
     * @param ids
     * @param newStatus
     */
    @Override
    public void updateNewStatus(Integer[] ids, Integer newStatus) {
        List<Integer> list = Arrays.asList(ids);
        pmsProductApi.updateNewStatus(list, newStatus);
    }

    /**
     * 批量上下架商品
     *
     * @param ids
     * @param publishStatus
     */
    @Override
    public void updatePublishStatus(Integer[] ids, Integer publishStatus) {
        List<Integer> list = Arrays.asList(ids);
        pmsProductApi.updatePublishStatus(list, publishStatus);
    }


    /**
     * 批量推荐商品
     *
     * @param ids
     * @param recommendStatus
     */
    @Override
    public void updateRecommendStatus(Integer[] ids, Integer recommendStatus) {
        List<Integer> list = Arrays.asList(ids);
        pmsProductApi.updateRecommendStatus(list, recommendStatus);

    }


    /**
     * 根据商品id获取商品编辑信息
     *
     * @param id
     * @return
     */
    @Override
    public PmsProductParam findProductById(Integer id) {
        PmsProductParam pmsProductParam = new PmsProductParam();
        // 获取商品基本信息
        PmsProduct pmsProduct = pmsProductApi.findById(id);
        Long productId = pmsProduct.getId();
        BeanUtils.copyProperties(pmsProduct, pmsProductParam);

        // 获取商品会员价格设置
        List<PmsMemberPrice> pmsMemberPriceList = pmsMemberPriceApi.findByProductId(productId);
        pmsProductParam.setMemberPriceList(pmsMemberPriceList);

        // 获取优选专区和商品的关系
        List<CmsPrefrenceAreaProductRelation> cmsPrefrenceAreaProductRelationList = cmsPrefrenceAreaProductRelationApi.findByProductId(productId);
        pmsProductParam.setPrefrenceAreaProductRelationList(cmsPrefrenceAreaProductRelationList);

        // 获取商品参数及自定义规格属性
        List<PmsProductAttributeValue> pmsProductAttributeList = pmsProductAttributeValueApi.findByProductId(productId);
        pmsProductParam.setProductAttributeValueList(pmsProductAttributeList);

        // 获取商品满减价格设置
        List<PmsProductFullReduction> pmsProductFullReductionList = pmsProductFullReductionApi.findByProductId(productId);
        pmsProductParam.setProductFullReductionList(pmsProductFullReductionList);

        // 获取商品阶梯价格设置
        List<PmsProductLadder> pmsProductLadderList = pmsProductLadderApi.findByProductId(productId);
        pmsProductParam.setProductLadderList(pmsProductLadderList);

        // 获取商品的sku库存信息
        List<PmsSkuStock> pmsSkuStockList = pmsSkuStockApi.findByProductId(productId);
        pmsProductParam.setSkuStockList(pmsSkuStockList);

        // 获取专题和商品关系
        List<CmsSubjectProductRelation> cmsSubjectProductRelationList = cmsSubjectProductRelationApi.findByProductId(productId);
        pmsProductParam.setSubjectProductRelationList(cmsSubjectProductRelationList);

        // 根据PmsProductCategoryApi的Id查询PmsProductCategoryApi
        PmsProductCategory pmsProductCategory = pmsProductCategoryApi.getProductCategoryById(pmsProduct.getProductCategoryId());
        pmsProductParam.setCateParentId(pmsProductCategory.getParentId());

        return pmsProductParam;
    }

    /**
     * 更新商品信息
     *
     * @param id
     * @param pmsProductParam
     */
    @Override
    public void update(Long id, PmsProductParam pmsProductParam) {
        // 创建并保存商品信息
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(pmsProductParam, pmsProduct);
        // 更新商品基本信息
        pmsProductApi.update(pmsProduct);
        Long productId = pmsProduct.getId();

        // 更新商品会员价格设置
        List<PmsMemberPrice> memberPriceList = pmsProductParam.getMemberPriceList();
        if (!memberPriceList.isEmpty()){
            pmsMemberPriceApi.deleteByProductId(productId);
            for (PmsMemberPrice price : memberPriceList) {
                price.setProductId(productId);
            }
            pmsMemberPriceApi.saveList(memberPriceList);
        }

        // 更新优选专区和商品的关系
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = pmsProductParam.getPrefrenceAreaProductRelationList();
        if (! prefrenceAreaProductRelationList.isEmpty()){
            cmsPrefrenceAreaProductRelationApi.deleteByProductId(productId);
            for (CmsPrefrenceAreaProductRelation relation : prefrenceAreaProductRelationList) {
                relation.setProductId(productId);
            }
            cmsPrefrenceAreaProductRelationApi.saveList(prefrenceAreaProductRelationList);
        }

        // 更新商品参数及自定义规格属性
        List<PmsProductAttributeValue> productAttributeValueList = pmsProductParam.getProductAttributeValueList();
        if (! productAttributeValueList.isEmpty()){
            pmsProductAttributeValueApi.deleteByProductId(productId);
            for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
                pmsProductAttributeValue.setProductId(productId);
            }
            pmsProductAttributeValueApi.saveList(productAttributeValueList);
        }

        // 更新商品满减价格设置
        List<PmsProductFullReduction> productFullReductionList = pmsProductParam.getProductFullReductionList();
        if (! productFullReductionList.isEmpty()){
            pmsProductFullReductionApi.deleteByProductId(productId);
            for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
                pmsProductFullReduction.setProductId(productId);
            }
            pmsProductFullReductionApi.saveList(productFullReductionList);
        }

        // 更新商品阶梯价格设置
        List<PmsProductLadder> productLadderList = pmsProductParam.getProductLadderList();
        if (!productLadderList.isEmpty()){
            pmsProductLadderApi.deleteByProductId(productId);
            for (PmsProductLadder pmsProductLadder : productLadderList) {
                pmsProductLadder.setProductId(productId);
            }
            pmsProductLadderApi.saveList(productLadderList);
        }

        // 更新商品的sku库存信息
        List<PmsSkuStock> skuStockList = pmsProductParam.getSkuStockList();
        if (!skuStockList.isEmpty()){
            pmsSkuStockApi.deleteByProductId(productId);
            for (PmsSkuStock pmsSkuStock : skuStockList) {
                pmsSkuStock.setProductId(productId);
            }
            pmsSkuStockApi.saveList(skuStockList);
        }

        // 更新专题和商品关系
        List<CmsSubjectProductRelation> subjectProductRelationList = pmsProductParam.getSubjectProductRelationList();
        if (!subjectProductRelationList.isEmpty()){
            cmsSubjectProductRelationApi.deleteByProductId(productId);
            for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
                cmsSubjectProductRelation.setProductId(productId);
            }
            cmsSubjectProductRelationApi.saveList(subjectProductRelationList);
        }
    }
}

